export function Qq(key) {
	return new Promise(function (resolve, reject) {
		window.init = function () {
			resolve(window.qq)//注意这里
		}
		let script = document.createElement("script");
		script.type = "text/javascript";
		script.src = "http://map.qq.com/api/js?v=2.exp&callback=init&key="+key;
		script.onerror = reject;
		document.head.appendChild(script);
	})
}

export const qqMapUtil = {
	myapp: null,
	markers: [],
	searchService: {},
	//初始化地图函数  自定义函数名init
	async init(myapp, latLng, hasLatLng = false,key) {
		const self = this;
		const qq = await Qq(key);
		self.myapp = myapp;
		//定义map变量 调用 qq.maps.Map() 构造函数   获取地图显示容器
		let center = new qq.maps.LatLng(latLng[0], latLng[1])
		let map = new qq.maps.Map(document.getElementById("container"), {
			center,      // 地图的中心地理坐标。
			zoom: 15                                                 // 地图的中心地理坐标。
		});
		//创建marker
		let marker = new qq.maps.Marker({
			position: center,
			map: map,
			draggable: true
		});
		//设置Marker停止拖动事件
		qq.maps.event.addListener(marker, 'dragend', function (e) {
			self.checkPos(e.latLng, myapp);
		});
		//添加监听事件
		qq.maps.event.addListener(map, 'click', function (e) {
			// 设置标记位置
			marker.setMap(null);
			marker = new qq.maps.Marker({
				map: map,
				position: e.latLng,
				draggable: true
			});
			self.checkPos(e.latLng, myapp);
			//设置Marker停止拖动事件
			qq.maps.event.addListener(marker, 'dragend', function (e) {
				self.checkPos(e.latLng, myapp);
			});
		});


		let latlngBounds = new qq.maps.LatLngBounds();
		//设置Poi检索服务，用于本地检索、周边检索
		self.searchService = new qq.maps.SearchService({
			//设置搜索范围为北京
			//location: myapp.currentCity,
			//设置搜索页码为1
			//pageIndex: 1,

			//设置展现查询结构到infoDIV上
			panel: document.getElementById('infoDiv'),
			//设置动扩大检索区域。默认值true，会自动检索指定城市以外区域。
			autoExtend: false,
			//检索成功的回调函数
			complete: function (results) {
				//设置回调函数参数
				let pois = results.detail.pois;
				for (let i = 0, l = pois.length; i < l; i++) {
					let poi = pois[i];
					//扩展边界范围，用来包含搜索到的Poi点
					latlngBounds.extend(poi.latLng);
					let marker = new qq.maps.Marker({
						map: map,
						position: poi.latLng,
					});
					// 设置标题 鼠标经过时自动显示
					marker.setTitle(poi.name);
					self.markers.push(marker);

					map.setCenter(poi.latLng);
				}
				//调整地图视野
				map.fitBounds(latlngBounds);
			},
			//若服务请求失败，则运行以下函数
			error: function () {
				alert("出错了。");
			}
		});

		if (!myapp.citylocation) {
			self.citylocation(myapp, map, marker);
			myapp.citylocation.searchLocalCity();
		}
		return map;
	},
	// 城市定位
	citylocation: function (myapp, map, marker) {
		let self = this;
		//设置城市信息查询服务
		let citylocation = new qq.maps.CityService();
		//请求成功回调函数
		citylocation.setComplete(function (result) {
			// 设置标记位置
			let latLng = result.detail.latLng;

			myapp.changeDefaultLatlng([latLng.lat, latLng.lng], false);
			//myapp.mapCenter=result.detail.latLng;
			if (result.detail.detail) {
				let citys = result.detail.detail.split(',');
				if (citys.length > 3) {
					myapp.currentCity = citys.splice(citys.length - 3, 1)[0];
					myapp.currentProvince = citys.splice(citys.length - 2, 1)[0];
				} else {
					myapp.currentCity = citys[0];
					myapp.currentProvince = citys[1];
				}
			} else {
				myapp.currentCity = result.detail.name;
				myapp.currentProvince = result.detail.name;
			}
			self.searchService.setLocation(result.detail.name, self.searchService);
			// 设置标记位置
			let center = new qq.maps.LatLng(latLng.lat, latLng.lng)
			map.setCenter(center);
			let marker = new qq.maps.Marker({
				map: map,
				position: center,
				draggable: true
			});
			self.checkPos(center, myapp);
			//设置Marker停止拖动事件
			qq.maps.event.addListener(marker, 'dragend', function (e) {
				self.checkPos(e.latLng, myapp);
			});
		});
		myapp.citylocation = citylocation;
	},
	// 用户点击地图或者移动标注点结束
	checkPos: function (latLng, myapp) {
		myapp = myapp || this.myapp;
		myapp.latLng = latLng;
	},
	//搜索（检索服务）
	searchKeyword: function (keyword, searchService, markers) {
		if (!keyword) {
			return;
		}
		searchService = searchService || this.searchService;
		markers = markers || this.markers;
		this.clearOverlays(markers);
		//根据输入的关键字在搜索范围内检索
		searchService.search(keyword);
	},
	//根据输入的城市设置搜索范围
	setLocation: function (city, searchService) {
		if (!city) {
			return;
		}
		searchService = searchService || this.searchService;
		searchService.setLocation(city);
	},
	//清除地图上的marker
	clearOverlays: function (overlays) {
		overlays = overlays || this.markers;
		let overlay;
		while (overlay = overlays.pop()) {
			overlay.setMap(null);
		}
	},
}

